#include <bits/stdc++.h>
using namespace std;

#define NAME "color"
const int N = 2e5+5,  mod = 998244353;

using LL = long long;
//#define int long long

int g[N];

int a[N];

int n, maxv;
int f[4000009];


#define getn(A, B)  f[(A)*maxv+(B)]

void solve() {
    maxv = 0; scanf("%d", &n);
    for (int i = 0; i < n; i++) scanf("%d", &a[i]), maxv = max(maxv, a[i]);
    
    for (int i = 0; i < (maxv*n); i++) f[i] =-0x3f3f3f3f;
    g[0]=-0x3f3f3f3f;

    int ans0=0;
    for (int i = 1; i < n; i++) {
        g[i] = -0x3f3f3f3f;
        for (int j = 1; j <= maxv; j++) {
            getn(i, j) = getn(i-1, j) + ((a[i]==a[i-1]?a[i]:0));
            g[i] = max(g[i], getn(i,j));
        }
        getn(i,a[i-1])=max(getn(i, a[i-1]), max(ans0, max(g[i-1], getn(i-1, a[i])+a[i])));
        g[i] = max(g[i], getn(i,a[i-1]));
        if (a[i] == a[i-1]) ans0 += a[i];
    }

    printf("%d\n", max(0,g[n-1]));
}

signed main() {
    freopen(NAME ".in", "r", stdin);
    freopen(NAME ".out", "w", stdout);
    
    int t; scanf("%d", &t);
    while (t--) {
        solve();
    }

    return 0;
}